//
//  StoreDetailPackageVCell.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/12/4.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "StoreDetailPackageVCell.h"
#import "ZZLayout.h"
#import "PackageListCell.h"
#import "CommonPackageModel.h"
@interface StoreDetailPackageVCell ()<UICollectionViewDataSource, UICollectionViewDelegate, ZZLayoutDelegate>
@property(nonatomic, strong) UICollectionView *collectionView;
@end

@implementation StoreDetailPackageVCell
- (void)makeUI {
    [super makeUI];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.frame = CGRectMake(0, 0, kScreenWidth, 500);
}
- (void)setData:(NSArray<CommonPackageModel *> *)data {
    _data = data;
    [self.collectionView reloadData];
    self.collectionView.height = [StoreDetailPackageVCell calculateHeight:data];
}
+ (CGFloat)calculateHeight:(NSArray<CommonPackageModel *> *)data {
    NSInteger row = data.count % 2 == 0 ? data.count / 2 : (data.count + 1) / 2;
    return row * (ceil((kScreenWidth - 34) * 0.5 * 0.94) + 84 + 10) - 10;
}
#pragma mark - UICollectionViewDelegate&&Data
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PackageListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.data2 = self.data[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[TT_JumpManager sharedManager] jumpWithDetailModel:self.data[indexPath.row] completion:^(BOOL success, NSString * _Nonnull message) {}];
}
#pragma mark - ZZLayoutDelegate
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //返回item的高
    return ceil((kScreenWidth - 34) * 0.5 * 0.94) + 84;
}
- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {
    //每个区有几列
    return 2;
}
- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    //设置每个区的边距
    return UIEdgeInsetsMake(0, 12, 0, 12);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {
    //设置每个区的行间距
    return 10;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {
    //设置每个区的列间距
    return 10;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {
    //设置区头的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {
    //设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}
- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {
    //本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {
    //设置不同分区的不同背景颜色
    return UIColor.whiteColor;
}
#pragma mark - lazy
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        ZZLayout *layout = [[ZZLayout alloc]initWith:ZZLayoutFlowTypeVertical delegate:self];
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[PackageListCell class] forCellWithReuseIdentifier:@"Cell"];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.bounces = NO;
        if (@available(iOS 11.0, *)) {
            collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView = collectionView;
    }
    return _collectionView;
}
@end
